package com.muyu.vehicle.aop;

import com.muyu.vehicle.domain.VehicleLog;
import com.muyu.vehicle.service.VehicleLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect
public class VehicleAop {
    @Autowired
    VehicleLogService vehicleLogService;
    @Around("execution(* com.muyu.vehicle.controller.*.*(..))")
    public Object aop(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取方法名称
        String name = joinPoint.getSignature().getName();
        System.out.println("获取方法名称"+name);
        //获取原方法的入参
        Object[] args = joinPoint.getArgs();
        System.out.println("获取原方法的入参"+args);
        //执行原方法
        Object proceed = joinPoint.proceed();
        System.out.println("原方法的返回值"+proceed);
        //原方法的执行时间
        Date date = new Date();
        System.out.println("原方法的执行时间"+date);
        VehicleLog vehicleLog = new VehicleLog();
        vehicleLog.setName(name);
        vehicleLog.setArgs(args.toString());
        vehicleLog.setProceed(proceed.toString());
        vehicleLog.setTimes(date);
        vehicleLogService.add(vehicleLog);
        return proceed;
    }
}
